/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.confiteriofuentes.modelo;

import com.confiteriafuentes.modelo.solicitud.Cliente;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;

/**
 *
 * @author Administrador
 */
public class ClienteDAO implements IDAO<Cliente>{
    
    private Model model;
    private PreparedStatement psInsert;
    private PreparedStatement psDelete;
    
    public ClienteDAO() throws SQLException {
        model = Model.getInstance();
    }

    public void insert(Cliente t) throws SQLException {
        if (psInsert == null) {
            String sqlInsert = "INSERT INTO cliente (codigo_cliente,nombre, telefono, email, direccion) VALUES (?, ?, ?, ?,?)";
            psInsert = model.getCon().prepareStatement(sqlInsert);
        }

       psInsert.setInt(1, t.getCodigo());
       psInsert.setString(2, t.getNombre());
       psInsert.setString(3, t.getTelefono());
       psInsert.setString(4,t.getEmail());
       psInsert.setString(5,t.getDireccion());

        psInsert.executeUpdate();
    }




    public Cliente find(Integer id) throws SQLException {
                  Connection conex=model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
                Cliente c=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM cliente WHERE codigo_cliente=?");
                            System.out.println(id.intValue());
                        pstmt.setInt(1, id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next()){

                      c=load(rs);
                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return c;
    }

    public  Cliente  load(ResultSet rs) throws SQLException {
   Cliente  des= new Cliente();
   des.setCodigo(rs.getInt(1));
   des.setNombre(rs.getString(2));
    des.setTelefono(rs.getString(3));
       des.setEmail(rs.getString(4));
   des.setDireccion(rs.getString(5));

 


 return des;
}


    public void delete(Cliente t) throws SQLException {
      
     if (psDelete == null) {
            String sqlInsert = "DELETE * FROM Cliente (codigo,nombre, telefono, email, direccion) VALUES (?, ?, ?, ?,?)";
            psInsert = model.getCon().prepareStatement(sqlInsert);
        }

       psDelete.setInt(1, t.getCodigo());
       psDelete.setString(2, t.getNombre());
       psDelete.setString(3, t.getTelefono());
       psDelete.setString(4,t.getEmail());
       psDelete.setString(5,t.getDireccion());

       
    }
   

    public void update(Cliente t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Cliente> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
